import com.kubepartners.impactmodel.model.Graph
import com.kubepartners.impactmodel.engine.EngineImpl
import com.kubepartners.impactmodel.translator.graphml.GraphmlDomWriter
import org.springframework.web.multipart.MultipartHttpServletRequest
import org.springframework.web.multipart.commons.CommonsMultipartFile
          
class GraphController {
	com.kubepartners.impactmodel.translator.GraphManager gtf
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
    	def graphs = []
    	def p = ~/.*graphml/
		new File(servletContext.getRealPath("/xml/")).eachFileMatch(p) { f ->
			graphs << f
		}
        [ graphList: graphs ]
    }

    def show = {
        def graph = gtf.read(servletContext.getRealPath("/xml/${params.file}.graphml"));
        if(!graph) {
            flash.message = "Graph not found with id ${params.file}"
            redirect(action:list)
        }
        else { return [ graph : graph ] }
    }

    def delete = {
        new File(servletContext.getRealPath("/xml/${params.file}.graphml")).delete()
        redirect(action:list)
    }

    def edit = {
        def graph = gtf.read(servletContext.getRealPath("/xml/${params.file}.graphml"));
        if(!graph) {
            flash.message = "Graph not found with id ${params.file}"
            redirect(action:list)
        }
        else { return [ graph : graph ] }
    }

    def update = {
    	def engine = new EngineImpl()
    	def graph = gtf.read(servletContext.getRealPath("/xml/${params.file}.graphml"));
    	params.each { key, value ->
    		def n = graph.findNodeById(key)
    		if (n) {
    			n.vertexCapacity = Double.parseDouble(value)
    		}
    	}
    	engine.graph = graph
    	engine.calculate()
        render(view:'edit',model:[graph:graph])
    }
    
    def export = {
    	def engine = new EngineImpl()
    	def graph = gtf.read(servletContext.getRealPath("/xml/${params.file}.graphml"));
    	params.each { key, value ->
    		def n = graph.findNodeById(key)
    		if (n) {
    			n.vertexCapacity = Double.parseDouble(value)
    		}
    	}
    	engine.graph = graph
    	engine.calculate()
    	def gw = new GraphmlDomWriter();
    	response.contentType = "application/yed"
    	response.setHeader("Content-Disposition","inline; filename=graph.graphml");
        gw.write(graph, response.outputStream);
    }

    def create = {
    }
    
    def showErrors = {
    	MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
		CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile("file");
		def graph = gtf.read(file.getInputStream());
       	def engine = new EngineImpl();
    	engine.graph = graph
    	def err = engine.isConsistent(graph);
    	render(view:'showErrors',model:[graph:graph])
    }
    
    def save = {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
		CommonsMultipartFile file = (CommonsMultipartFile)multiRequest.getFile("file");
		def graph = gtf.read(file.getInputStream());
		gtf.write(graph, servletContext.getRealPath("/xml/" + file.originalFilename));
		redirect(action:list)
    }
}